package com.ymttest.database.sqlwapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.database.SqlManager;
import com.ymttest.database.model.PPBussinessOrder;
import com.ymttest.database.model.PPCompensateProcessInfo;
import com.ymttest.database.model.PPPayment;
import com.ymttest.database.model.RefundMiscRequestLog;
import com.ymttest.database.model.RefundRequest;
import com.ymttest.utils.EnvSetup;

/************************************************************************************
 * @File name :DefaultWapper.java
 * @Author : zhouyi
 * @Date : 2015年6月1日
 * @Copyright : 洋码头
 ************************************************************************************/
public class PaymentDefaultWapper {
    static SqlManager manage;

    public PaymentDefaultWapper() {
        if (manage == null || manage.getEnv() != "ymt" + EnvSetup.getEnv() || manage.isClosed()) {
            manage = new SqlManager("ymt" + EnvSetup.getEnv());
        }
    }

    /**
     * 使用 BussinessOrderId 查询支付单信息
     * 
     * @param BussinessOrderId
     * @return
     * @throws InterruptedException
     */
    public List<PPPayment> selectPPPaymentByBussinessOrderId(String bussinessOrderId) throws InterruptedException {
        @SuppressWarnings("unchecked")
        Map m = new HashMap<String, String>();
        m.put("bussinessOrderId", bussinessOrderId);
        List<PPPayment> pia = manage.getSelectResults(
                "com.ymttest.database.mapping.PaymentMapper.selectPPPaymentByBussinessOrderId", m);
        manage.commit();
        return pia;
    }

    /**
     * 查询商户订单
     * 
     * @param tracdId
     * @return
     * @throws InterruptedException
     */
    public List<PPBussinessOrder> selectPPBussinessorderByTraceId(String tracdId) throws InterruptedException {
        @SuppressWarnings("unchecked")
        Map m = new HashMap<String, String>();
        m.put("tracdId", tracdId);
        List<PPBussinessOrder> pia = manage.getSelectResults(
                "com.ymttest.database.mapping.PaymentMapper.selectPPBussinessorderByTraceId", m);
        manage.commit();
        return pia;
    }

    /**
     * 查询商户订单
     * 
     * @param orderId
     * @return
     * @throws InterruptedException
     */
    public List<PPBussinessOrder> selectPPBussinessorderByOrderId(String orderId) throws InterruptedException {
        @SuppressWarnings("unchecked")
        Map m = new HashMap<String, String>();
        m.put("orderId", orderId);
        List<PPBussinessOrder> pia = manage.getSelectResults(
                "com.ymttest.database.mapping.PaymentMapper.selectPPBussinessorderByOrderId", m);
        manage.commit();
        return pia;
    }

    /**
     * 查询退款信息
     * 
     * @param pid PaymentId
     * @return
     * @throws InterruptedException
     */
    public List<RefundRequest> selectRefundRequestByPaymentId(String pid) throws InterruptedException {
        @SuppressWarnings("unchecked")
        Map m = new HashMap<String, String>();
        m.put("pid", pid);
        List<RefundRequest> pia = manage.getSelectResults(
                "com.ymttest.database.mapping.PaymentMapper.selectRefundRequestByPaymentId", m);
        manage.commit();
        return pia;
    }

    /**
     * 查询退款信息
     * 
     * @param pid PaymentId
     * @return
     * @throws InterruptedException
     */
    public List<Map> selectRefundRequestByTraceId(String tid) throws InterruptedException {
        @SuppressWarnings("unchecked")
        Map m = new HashMap<String, String>();
        m.put("tid", tid);
        List<Map> pia = manage.getSelectResults(
                "com.ymttest.database.mapping.PaymentMapper.selectRefundRequestByTraceId", m);
        manage.commit();
        return pia;
    }

    /**
     * RefundBatchNo查询退款记录
     * 
     * @param rno
     * @return
     * @throws InterruptedException
     */
    public List<Map> selectRefundRequestByRefundBatchNo(String rno) throws InterruptedException {
        @SuppressWarnings("unchecked")
        Map m = new HashMap<String, String>();
        m.put("rno", rno);
        List<Map> pia = manage.getSelectResults(
                "com.ymttest.database.mapping.PaymentMapper.selectRefundRequestByRefundBatchNo", m);
        manage.commit();
        return pia;
    }

    public List<Map> selectRefundRequestByRefundId(String rid) throws InterruptedException {
        @SuppressWarnings("unchecked")
        Map m = new HashMap<String, String>();
        m.put("rid", rid);
        List<Map> pia = manage.getSelectResults(
                "com.ymttest.database.mapping.PaymentMapper.selectRefundRequestByRefundId", m);
        manage.commit();
        return pia;
    }

    /**
     * 查询发货信息
     * 
     * @param pid PaymentId
     * @return
     * @throws InterruptedException
     */
    public List<Map> selectCompensateProcessInfoByPaymentId(String pid) throws InterruptedException {
        @SuppressWarnings("unchecked")
        Map m = new HashMap<String, String>();
        m.put("pid", pid);
        List<Map> pia = manage.getSelectResults(
                "com.ymttest.database.mapping.PaymentMapper.selectCompensateProcessInfoByPaymentId", m);
        manage.commit();
        return pia;
    }

    /**
     * 查询accounting信息
     * 
     * @param {bid=RefundBatchNo}
     * @return
     */
    public List<Map> selectPPAccoutingLogByBizno(String bid) throws InterruptedException {
        @SuppressWarnings("unchecked")
        Map m = new HashMap<String, String>();
        m.put("bid", bid);
        List<Map> pia = manage.getSelectResults(
                "com.ymttest.database.mapping.PaymentMapper.selectPPAccoutingLogByBizno", m);
        manage.commit();
        return pia;
    }
    public List<Map> selectMockHttpRecordByMockId(String mockid) throws InterruptedException {
        @SuppressWarnings("unchecked")
        Map m = new HashMap<String, String>();
        m.put("mockid", mockid);
        List<Map> pia = manage.getSelectResults(
                "com.ymttest.database.mapping.PaymentMapper.selectMockHttpRecordByMockId", m);
        manage.commit();
        return pia;
    }

    /**
     * 查询支付单
     * 
     * @param paymentId
     * @return
     * @throws InterruptedException
     */
    public List<PPCompensateProcessInfo> selectPPCompensateProcessInfoByCorrelateIdAndCompensateType(String paymentId,
            int compensateType) throws InterruptedException {
        Map m = new HashMap<String, String>();
        m.put("paymentId", paymentId);
        m.put("compensateType", compensateType);

        List<PPCompensateProcessInfo> pia =
                manage.getSelectResults(
                        "com.ymttest.database.mapping.PPCompensateProcessInfoMapper.selectPPCompensateProcessInfoByCorrelateIdAndCompensateType",
                        m);
        return pia;
    }

    /**
     * 查询支付单
     * 
     * @param paymentId
     * @return
     * @throws InterruptedException
     */
    public List<PPCompensateProcessInfo> selectPPCompensateProcessInfoByCorrelateId(String paymentId)
            throws InterruptedException {
        Map m = new HashMap<String, String>();
        m.put("paymentId", paymentId);

        List<PPCompensateProcessInfo> pia =
                manage.getSelectResults(
                        "com.ymttest.database.mapping.PPCompensateProcessInfoMapper.selectPPCompensateProcessInfoByCorrelateId",
                        m);
        manage.commit();
        return pia;
    }

    /**
     * 查询退款申请
     * 
     * @param paymentId
     * @return
     * @throws InterruptedException
     */
    public List<RefundMiscRequestLog> selectRefundMiscRequestLogByCorrelateId(String paymentid)
            throws InterruptedException {
        Map m = new HashMap<String, String>();
        m.put("CorrelateId", paymentid);
        List<RefundMiscRequestLog> pia = manage.getSelectResults(
                "com.ymttest.database.mapping.RefundMiscRequestLogMapper.selectRefundMiscRequestLogByCorrelateId", m);
        manage.commit();
        return pia;
    }

    /**
     * 查询退款申请
     * 
     * @param paymentId
     * @return
     * @throws InterruptedException
     */
    public List<RefundMiscRequestLog> selectRefundMiscRequestLogByRefundBatchNo(String rno)
            throws InterruptedException {
        Map m = new HashMap<String, String>();
        m.put("rno", rno);
        List<RefundMiscRequestLog> pia = manage.getSelectResults(
                "com.ymttest.database.mapping.RefundMiscRequestLogMapper.selectRefundMiscRequestLogByRefundBatchNo", m);
        manage.commit();
        return pia;
    }

    /**
     * 查询退款申请
     * 
     * @param paymentId
     * @return
     * @throws InterruptedException
     */
    public List<RefundRequest> selectRefundRequestByOrderid(String orderid) throws InterruptedException {
        Map m = new HashMap<String, String>();
        m.put("orderid", orderid);
        List<RefundRequest> pia = manage.getSelectResults(
                "com.ymttest.database.mapping.RefundRequestMapper.selectRefundRequestByOrderid", m);
        manage.commit();
        return pia;
    }

    /**
     * 查询退款申请单
     * 
     * @param paymentId
     * @return
     * @throws InterruptedException
     */
    public List<RefundRequest> selectRefundRequestByTradeNo(String tradeNo) throws InterruptedException {
        Map m = new HashMap<String, String>();
        m.put("tradeNo", tradeNo);
        List<RefundRequest> pia = manage.getSelectResults(
                "com.ymttest.database.mapping.RefundRequestMapper.selectRefundRequestByTradeNo", m);
        manage.commit();
        return pia;
    }

    public List<RefundRequest> selectRefundRequestByPaymentid(String paymentid) throws InterruptedException {
        Map m = new HashMap<String, String>();
        m.put("paymentid", paymentid);
        List<RefundRequest> pia = manage.getSelectResults(
                "com.ymttest.database.mapping.RefundRequestMapper.selectRefundRequestByPaymentid", m);
        manage.commit();
        return pia;
    }

    /**
     * 更新退款状态
     * 
     * @param refundno
     * @param status
     * @return
     * @throws InterruptedException
     */
    public int updateRefundStatus(String refundno, String status) throws InterruptedException {
        Logger.comment("refundbatchno：" + refundno + "更新退款状态为：" + status);
        int i = 0;
        if (refundno != null && refundno.length() > 0 && status != null && status.length() > 0) {
            Map m = new HashMap<String, String>();
            m.put("refundno", refundno);
            m.put("status", status);
            i = manage.update("com.ymttest.database.mapping.PaymentMapper.updateRefundStatus", m);
            manage.commit();
        }
        return i;
    }
    
    /**
     * 更新RefundNo
     * @param rno
     * @param pid
     * @return
     * @throws InterruptedException
     */
    public int updateRefundNo(String rno, String pid) throws InterruptedException {
        int i = 0;
        if (rno != null && rno.length() > 0 && pid != null && pid.length() > 0) {
            Map m = new HashMap<String, String>();
            m.put("pid", pid);
            m.put("rno", rno);
            i = manage.update("com.ymttest.database.mapping.PaymentMapper.updateRefundNo", m);
            manage.commit();
        }
        return i;
    }
    
    /**
     * 更新退款状态
     * 
     * @param refundno
     * @param status
     * @return
     * @throws InterruptedException
     */
    public int updateRefundApproveStatus(String refundno, String status) throws InterruptedException {
        Logger.comment("refundbatchno：" + refundno + "更新退款审核状态为：" + status);
        int i = 0;
        if (refundno != null && refundno.length() > 0 && status != null && status.length() > 0) {
            Map m = new HashMap<String, String>();
            m.put("refundno", refundno);
            m.put("status", status);
            i = manage.update("com.ymttest.database.mapping.PaymentMapper.updateRefundApproveStatus", m);
            manage.commit();
        }
        return i;
    }
    public int updateRefundSoftDeleteFlag(String refundno, String status) throws InterruptedException {
        Logger.comment("refundbatchno：" + refundno + "更新审核状态为：" + status);
        int i = 0;
        if (refundno != null && refundno.length() > 0 && status != null && status.length() > 0) {
            Map m = new HashMap<String, String>();
            m.put("refundno", refundno);
            m.put("status", status);
            i = manage.update("com.ymttest.database.mapping.PaymentMapper.updateRefundSoftDeleteFlag", m);
            manage.commit();
        }
        return i;
    }

    /**
     * @param pid paymentid
     * @param status  PayStatus
     * @return
     * @throws InterruptedException
     */
    public int updateNotifyStatus(String pid,String status) throws InterruptedException {
    	Logger.comment("paymentid："+pid+"更新第三方异步通知状态为："+status);
    	int i=0;
    	if(pid!=null&&pid.length()>0&&status!=null&&status.length()>0){
    		Map m = new HashMap<String, String>();
    		m.put("pid", pid);
    		m.put("status", status);
    		i = manage.update("com.ymttest.database.mapping.PaymentMapper.updateNotifyStatus", m);
    		manage.commit();
    	}
        return i;
    }
    /**
     * @param pid paymentid
     * @param status  PayStatus
     * @return
     * @throws InterruptedException
     */
    public int updatePayStatus(String pid,String status) throws InterruptedException {
    	Logger.comment("paymentid："+pid+"更新支付状态为："+status);
    	int i=0;
    	if(pid!=null&&pid.length()>0&&status!=null&&status.length()>0){
    		Map m = new HashMap<String, String>();
    		m.put("pid", pid);
    		m.put("status", status);
    		i = manage.update("com.ymttest.database.mapping.PaymentMapper.updatePayStatus", m);
    		manage.commit();
    	}
        return i;
    }
    
    /**
     * 删除签约记录
     * @param uid
     * @return
     * @throws InterruptedException
     */
    public int deleteCmbAggrementByUId(String uid) throws InterruptedException {
    	Logger.comment("uid："+uid);
    	int i=0;
    	if(uid!=null&&uid.length()>0){
    		Map m = new HashMap<String, String>();
    		m.put("uid", uid);
    		i = manage.update("com.ymttest.database.mapping.PaymentMapper.deleteCmbAggrementByUId", m);
    		manage.commit();
    	}
        return i;
    }
    
    /**
     * 查询支付单
     * 
     * @param paymentId
     * @return
     * @throws InterruptedException
     */
    public List<PPPayment> selectPPPaymentByPaymentId(String paymentId) throws InterruptedException {
        Map m = new HashMap<String, String>();
        m.put("paymentId", paymentId);
        List<PPPayment> pia = manage.getSelectResults(
                "com.ymttest.database.mapping.PPPaymentMapper.selectPPPaymentByPaymentId", m);
        manage.commit();
        return pia;
    }
    public List<PPPayment> selectPPPaymentByTradingId(String tradid) throws InterruptedException {
        Map m = new HashMap<String, String>();
        m.put("tradid", tradid);
        List<PPPayment> pia = manage.getSelectResults(
                "com.ymttest.database.mapping.PaymentMapper.selectPPPaymentByTradingId", m);
        manage.commit();
        return pia;
    }
    public List<Map> selectCmbAggrementByUId(String uid) throws InterruptedException {
        @SuppressWarnings("unchecked")
        Map m = new HashMap<String, String>();
        m.put("uid", uid);
        List<Map> pia = manage.getSelectResults(
                "com.ymttest.database.mapping.PaymentMapper.selectCmbAggrementByUId", m);
        manage.commit();
        return pia;
    }

	public static void main(String args[]) {
		try {
			// List<PPPayment> ppm = new PaymentDefaultWapper()
			// .selectPPPaymentByBussinessOrderId("2BB48EB5-767D-4408-9D41-7270733CCF02");
			// for (PPPayment ppPayment : ppm) {
			// Logger.debug(FormatUtil.beanToGSONString(ppPayment));
			// }
			// List<PPBussinessOrder> ppb = new PaymentDefaultWapper()
			// .selectPPBussinessorderByTraceId("e4efcdce-3fde-4d9e-a1be-9196fb0ebcd0");
			// for (PPBussinessOrder ppBussinessorder : ppb) {
			// Logger.debug(FormatUtil.beanToGSONString(ppBussinessorder));
			// }

			System.out.println(new PaymentDefaultWapper()
					.selectPPPaymentByTradingId("21180988699"));

		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
